<?php
// Version
define('VERSION', '1.5.6.4');

// Configuration
if (file_exists('config.php')) {
    require_once('config.php');
}

// Install
if (!defined('DIR_APPLICATION')) {
    header('Location: install/index.php');
    exit;
}

// Startup
require_once(DIR_SYSTEM . 'startup.php');

// Application Classes
require_once(DIR_SYSTEM . 'library/customer.php');
require_once(DIR_SYSTEM . 'library/affiliate.php');
require_once(DIR_SYSTEM . 'library/currency.php');
require_once(DIR_SYSTEM . 'library/tax.php');
require_once(DIR_SYSTEM . 'library/weight.php');
require_once(DIR_SYSTEM . 'library/length.php');
require_once(DIR_SYSTEM . 'library/cart.php');
require_once(DIR_SYSTEM . 'library/curl.php');
require_once(DIR_SYSTEM . 'library/check.php');
require_once(DIR_SYSTEM . 'library/backend.php');
require_once(DIR_SYSTEM . 'library/mdsmssend.php');
require_once(DIR_SYSTEM . 'library/price.php');
// Registry
$registry = new Registry();

// Loader
$loader = new Loader($registry);
$registry->set('load', $loader);

// Config
$config = new Config();
$registry->set('config', $config);

// Database
$db = new DB(DB_DRIVER, DB_HOSTNAME, DB_USERNAME, DB_PASSWORD, DB_DATABASE);
$registry->set('db', $db);

// Request
$request = new Request();
$registry->set('request', $request);
// Session
$session = new Session();
$registry->set('session', $session);

//customer
$customer =new Customer($registry);
$registry->set('customer', $customer);

$registry->set('curl', new Curl($registry));

//backend
$backend =new Backend($registry);
//smssend
$mdsmssend =new Mdsmssend($registry);

// Settings
$query = $db->query("SELECT * FROM " . DB_PREFIX . "setting WHERE store_id = '0' OR store_id = '" . (int)$config->get('config_store_id') . "' ORDER BY store_id ASC");

foreach ($query->rows as $setting) {
    if (!$setting['serialized']) {
        $config->set($setting['key'], $setting['value']);
    } else {
        $config->set($setting['key'], unserialize($setting['value']));
    }
}

//trade_create_by_buyer 双接口 ,create_direct_pay_by_user 直接到帐，create_partner_trade_by_buyer 担保接口
$trade_type = $config->get('alipay_trade_type');

// 		log_result("Alipay :: exciting callback function.");
$oder_success = FALSE;
$seller_email = $config->get('alipay_seller_email'); // 商家邮箱
$partner = $config->get('alipay_partner'); //合作伙伴ID
$security_code = $config->get('alipay_security_code'); //安全检验码

$_input_charset = "utf-8";
//$_input_charset = "GBK";
$sign_type = "MD5";
$transport = 'http';

require_once("catalog/controller/payment/alipay_notify.php");
$alipay = new alipay_notify($partner,$security_code,$sign_type,$_input_charset,$transport);
$verify_result = $alipay->notify_verify();

if($verify_result) {
    $order_id = $_POST['out_trade_no'];
    $total=floor($_POST['total_fee']);
    $trade_status=$_POST['trade_status'];

    $query= $db->query("SELECT ro.status,c.email FROM " . DB_PREFIX . "recharge_order ro LEFT JOIN " . DB_PREFIX . "customer c ON(ro.customer_id=c.customer_id) WHERE ro.recharge_order_id=".$order_id."");
    //获得余额
    $balance=$backend->getbalance($query->row['email']);

    $order_status=$query->row['status'];
    if(!$order_status){
        if($trade_status == 'TRADE_FINISHED' ||$trade_status == 'TRADE_SUCCESS') {
            $db->query("UPDATE " . DB_PREFIX . "recharge_order SET status = '1' WHERE recharge_order_id=".$order_id."");
            $customer=$db->query("select * from " . DB_PREFIX . "customer WHERE customer_id=(SELECT customer_id FROM ".DB_PREFIX . "recharge_order WHERE recharge_order_id=".$order_id." )");
            $backend->recharge($customer->row['email'],$customer->row['password_plain'],(int)$total);

            $message="您本次充值".$total."元，目前的账户余额为".($total+$balance)."元。";
            $mdsmssend->send($customer->row['telephone'],$message);


            $subject='NeuCloud Apps - 充值成功';
            $message="您本次充值".$total."元，目前的账户余额为".($total+$balance)."元。 \n\n\n寄云科技。";

            $mail = new Mail();
            $mail->protocol = $config->get('config_mail_protocol');
            $mail->parameter = $config->get('config_mail_parameter');
            $mail->hostname = $config->get('config_smtp_host');
            $mail->username = $config->get('config_smtp_username');
            $mail->password = $config->get('config_smtp_password');
            $mail->port = $config->get('config_smtp_port');
            $mail->timeout = $config->get('config_smtp_timeout');
            $mail->setTo($customer->row['email']);
            $mail->setFrom($config->get('config_email'));
            $mail->setSender($config->get('config_name'));
            $mail->setSubject(html_entity_decode($subject, ENT_QUOTES, 'UTF-8'));
            $mail->setText(html_entity_decode($message, ENT_QUOTES, 'UTF-8'));
            $mail->send();
        }
        }else{
            echo 'error';
        }
}



?>